<template>
  <div class="joystick-container">
    <div class="joystick-area" ref="joystickArea">
      <div 
        class="joystick-handle" 
        ref="joystickHandle"
        :style="handleStyle"
        @touchstart="handleTouchStart"
        @touchmove="handleTouchMove"
        @touchend="handleTouchEnd"
        @mousedown="handleMouseDown"
      ></div>
      <div class="joystick-center"></div>
      <div class="joystick-crosshair">
        <div class="crosshair-line horizontal"></div>
        <div class="crosshair-line vertical"></div>
      </div>
    </div>
    
    <div class="joystick-info glass">
      <div class="info-item">
        <span class="info-label">X轴</span>
        <span class="info-value">{{ currentX }}</span>
      </div>
      <div class="info-item">
        <span class="info-label">Y轴</span>
        <span class="info-value">{{ currentY }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'

const props = defineProps({
  size: {
    type: Number,
    default: 240
  },
  maxDistance: {
    type: Number,
    default: 90
  }
})

const emit = defineEmits(['change', 'start', 'end'])

const joystickArea = ref(null)
const joystickHandle = ref(null)
const isDragging = ref(false)
const centerX = ref(0)
const centerY = ref(0)
const currentX = ref(0)
const currentY = ref(0)

// 计算摇杆手柄样式
const handleStyle = computed(() => {
  const x = centerX.value + currentX.value
  const y = centerY.value + currentY.value
  return {
    transform: `translate(${x}px, ${y}px)`
  }
})

// 初始化摇杆
onMounted(() => {
  if (joystickArea.value) {
    const rect = joystickArea.value.getBoundingClientRect()
    centerX.value = rect.width / 2 - 25 // 手柄半径25px
    centerY.value = rect.height / 2 - 25
  }
})

// 触摸开始
const handleTouchStart = (event) => {
  event.preventDefault()
  isDragging.value = true
  emit('start')
  updateJoystickPosition(event.touches[0])
}

// 触摸移动
const handleTouchMove = (event) => {
  event.preventDefault()
  if (isDragging.value) {
    updateJoystickPosition(event.touches[0])
  }
}

// 触摸结束
const handleTouchEnd = (event) => {
  event.preventDefault()
  isDragging.value = false
  resetJoystick()
  emit('end')
}

// 鼠标按下
const handleMouseDown = (event) => {
  event.preventDefault()
  isDragging.value = true
  emit('start')
  
  const handleMouseMove = (e) => {
    if (isDragging.value) {
      updateJoystickPosition(e)
    }
  }
  
  const handleMouseUp = () => {
    isDragging.value = false
    resetJoystick()
    emit('end')
    document.removeEventListener('mousemove', handleMouseMove)
    document.removeEventListener('mouseup', handleMouseUp)
  }
  
  document.addEventListener('mousemove', handleMouseMove)
  document.addEventListener('mouseup', handleMouseUp)
  
  updateJoystickPosition(event)
}

// 更新摇杆位置
const updateJoystickPosition = (event) => {
  if (!joystickArea.value) return
  
  const rect = joystickArea.value.getBoundingClientRect()
  const clientX = event.clientX || event.pageX
  const clientY = event.clientY || event.pageY
  
  const deltaX = clientX - rect.left - rect.width / 2
  const deltaY = clientY - rect.top - rect.height / 2
  
  const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY)
  
  if (distance > props.maxDistance) {
    const angle = Math.atan2(deltaY, deltaX)
    currentX.value = Math.cos(angle) * props.maxDistance
    currentY.value = Math.sin(angle) * props.maxDistance
  } else {
    currentX.value = deltaX
    currentY.value = deltaY
  }
  
  // 转换为-100到100的范围
  const normalizedX = Math.round((currentX.value / props.maxDistance) * 100)
  const normalizedY = Math.round((currentY.value / props.maxDistance) * -100) // Y轴反转
  
  emit('change', { x: normalizedX, y: normalizedY })
}

// 重置摇杆
const resetJoystick = () => {
  currentX.value = 0
  currentY.value = 0
  emit('change', { x: 0, y: 0 })
}

// 清理事件监听器
onUnmounted(() => {
  document.removeEventListener('mousemove', () => {})
  document.removeEventListener('mouseup', () => {})
})
</script>

<style scoped>
.joystick-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.joystick-area {
  position: relative;
  width: 240px;
  height: 240px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 3px solid var(--primary-color);
  box-shadow: var(--shadow-heavy);
  cursor: pointer;
  user-select: none;
  touch-action: none;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.joystick-area:hover {
  transform: scale(1.02);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
}

.joystick-handle {
  position: absolute;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: var(--gradient-primary);
  box-shadow: var(--shadow-heavy);
  border: 4px solid white;
  cursor: grab;
  transition: transform 0.1s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 3;
}

.joystick-handle:active {
  cursor: grabbing;
  transform: scale(1.1);
}

.joystick-center {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--primary-color);
  transform: translate(-50%, -50%);
  z-index: 1;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.joystick-crosshair {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.crosshair-line {
  position: absolute;
  background: rgba(99, 102, 241, 0.2);
  border-radius: 1px;
}

.crosshair-line.horizontal {
  top: 50%;
  left: 0;
  width: 100%;
  height: 2px;
  transform: translateY(-50%);
}

.crosshair-line.vertical {
  left: 50%;
  top: 0;
  width: 2px;
  height: 100%;
  transform: translateX(-50%);
}

.joystick-info {
  display: flex;
  gap: 32px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 16px;
  padding: 16px 24px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: var(--shadow-medium);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.joystick-info:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-heavy);
}

.info-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.info-label {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.info-value {
  font-size: 20px;
  color: var(--text-primary);
  font-weight: 800;
  min-width: 50px;
  text-align: center;
  background: var(--gradient-secondary);
  padding: 4px 8px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
}

/* 移动端适配 */
@media (max-width: 768px) {
  .joystick-area {
    width: 200px;
    height: 200px;
  }
  
  .joystick-handle {
    width: 44px;
    height: 44px;
  }
  
  .joystick-info {
    padding: 12px 20px;
    gap: 24px;
  }
  
  .info-value {
    font-size: 18px;
    min-width: 45px;
  }
}

/* 横屏适配 */
@media screen and (orientation: landscape) {
  .joystick-container {
    flex-direction: row;
    gap: 24px;
  }
  
  .joystick-area {
    width: 220px;
    height: 220px;
  }
  
  .joystick-handle {
    width: 48px;
    height: 48px;
  }
}

/* 动画效果 */
@keyframes pulse {
  0%, 100% { 
    transform: scale(1); 
    opacity: 1;
  }
  50% { 
    transform: scale(1.05); 
    opacity: 0.8;
  }
}

.joystick-area:active {
  animation: pulse 0.5s ease-in-out;
}
</style> 